$(function () {
  $('#header').load('./common/header.html')
  $('#footer').load('./common/footer.html')

  // 请求电影热门榜前10
  $.ajax({
    url: 'http://localhost:3000/index/movie',
    success: result => {
      // console.log(result);
      let str = ''
      for (const i of result.data) {
        str = str +
          `<li>
            <img src="${i.mimage}">
            <p class="titlescore">${i.mname} <strong>${i.mscore}</strong></p>
            <div class="alertinfo">
                <h5 class="alerttitle">${i.mname + '(' + i.mshowdate + ')'}</h5>
                <div class="alertscore">${i.mscore}</div>
                <div class="alerttag">
                  <span>${i.mlength + '分钟'}</span>
                  <span>${i.mdirector}</span>
                  <span>${i.mcategory}</span>
                  <span>${i.mactor}</span>
                </div>
                <p class="introduction">${i.mintroduction.slice(0, 120) + '......'}</p>
              </div>
            </li>`
      }

      $('#moviecontent ul').html(str)


      // 给li绑定点击事件
      $('#moviecontent ul li').each(function (i) {
        $(this).click(function () {
          const titlescore = $(this).find('.titlescore').text()
          const title = titlescore.split(' ')[0]
          // console.log(title);
          // 修改a链接的href值
          $(location).attr('href', `http://localhost:3000/movieinfo.html?mname=${title}`)
        })
      })


      // 鼠标移入弹出电影信息
      $('#moviecontent ul li').hover(function () {
        // console.log($(this).find('.alertinfo .alerttag span'));
        $(this).find('.alertinfo').css('display', 'block')
        $(this).siblings('li').find('.alertinfo').css('display', 'none')
      })
      // 鼠标移出，关闭弹出框
      $('#moviecontent ul li').mouseleave(function () {
        $(this).find('.alertinfo').css('display', 'none')
      })
    },
    error: err => {
      console.log(err);
    }
  })

  // 请求电影口碑榜前10
  $.ajax({
    url: 'http://localhost:3000/index/score',
    success: result => {
      // console.log(result);
      let str = ''
      for (const i in result.data) {
        str = str +
          `<li>
            <span>${i - -1}</span><a href="javascript: void(0)" class="scoretitle">${result.data[i].mname}</a>
          </li>`
      }
      $('.right_main .scorebillboard ul').html(str)

      // 给li绑定点击事件
      $('.right_main .scorebillboard ul li').each(function (i) {
        $(this).click(function () {
          const title = $(this).find('.scoretitle').text()
          // 修改a链接的href值
          $(location).attr('href', `http://localhost:3000/movieinfo.html?mname=${title}`)
        })
      })

    },
    error: err => {
      console.log(err);
    }
  })

})